/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.net.NetworkInfo;

public class NetworkUtils {
	
	public enum Status {
		UNAVAILABLE, AVAILABLE, CONNECTING, CONNECTED 
	}	

	public static Status getStatus(NetworkInfo info) {
	    Status status = Status.UNAVAILABLE;
	    if(info!=null) {
		    if(info.isAvailable())
		    	status = Status.AVAILABLE;
		    if(info.isConnectedOrConnecting())
		    	status = Status.CONNECTING;
		    if(info.isConnected())
		    	status = Status.CONNECTED;
	    }	    
	    return status;
	}
	
	public static int lookupHost(String hostname) {
	    InetAddress inetAddress;
	    try {
	        inetAddress = InetAddress.getByName(hostname);
	    } catch (UnknownHostException e) {
	        return -1;
	    }
	    byte[] addrBytes = inetAddress.getAddress();
	    return ((addrBytes[3] & 0xff) << 24)
	            | ((addrBytes[2] & 0xff) << 16)
	            | ((addrBytes[1] & 0xff) << 8)
	            |  (addrBytes[0] & 0xff);
	}

}
